package domain;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;


//客户端登录成功后从数据库拉取用户信息
/*
* 其中的用户状态用来标识是否可以匹配
* */
public class Player {
    public int playerID;
    private Socket socket;
    public ArrayList<String> handCards=new ArrayList<>();//持有的手牌
    private Socket connection;//和服务器进行连接
    private String userName;
    private String userMail;
    private int index;
    private String userIntegral;
    private boolean isLandlord=false;
    public BufferedReader reader;
    public BufferedWriter writer;
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Player(Socket socket) throws IOException {
        this.socket=socket;
        reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    }

    public boolean isLandlord() {
        return isLandlord;
    }

    public void setLandlord(boolean landlord) {
        isLandlord = landlord;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserMail() {
        return userMail;
    }

    public void setUserMail(String userMail) {
        this.userMail = userMail;
    }

    public String getUserIntegral() {
        return userIntegral;
    }

    public void setUserIntegral(String userIntegral) {
        this.userIntegral = userIntegral;
    }

    public String receiveDataFromServer() {
            String receivedData = null;
            BufferedReader reader = null;
            try {
                connection.setSoTimeout(500000);
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                receivedData = reader.readLine();
            } catch (IOException e) {
                System.out.println("接收数据失败: " + e.getMessage());
            }
            return receivedData;

    }

    public void receiveCardsFromServer(String card) {
        //按照空格分割
        String[]remaining=card.split(" ");
        //将remaining中的数据存入cards中
        for (String item : remaining) {
            handCards.add(item);
        }
    }
}
